package com.frame.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.frame.common.core.entity.SysDictType;
import com.frame.common.core.security.utils.SecurityUtils;
import com.frame.db.util.Condition;
import com.frame.db.util.Query;
import com.frame.stant.constans.UserConstants;
import com.frame.stant.utils.R;
import com.frame.system.service.ISysDictTypeService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据字典信息
 *
 * @author ruoyi
 */
@Api(tags = "字典类型")
@RestController
@RequestMapping("/system/dict/type")
@AllArgsConstructor
public class SysDictTypeController {

    private final ISysDictTypeService dictTypeService;

    @PreAuthorize("@custom.hasAnyAuthority('system:dict:list')")
    @GetMapping("/list")
    public R list(SysDictType dictType, Query query) {
        IPage<SysDictType> list = dictTypeService.selectDictTypeList(Condition.page(query),dictType);
        return R.success(list);
    }

//    @Log(title = "字典类型", businessType = BusinessType.EXPORT)
//    @PreAuthorize("@custom.hasAnyAuthority('system:dict:export')")
//    @GetMapping("/export")
//    public AjaxResult export(SysDictType dictType) {
//        List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
//        ExcelUtil<SysDictType> util = new ExcelUtil<SysDictType>(SysDictType.class);
//        return util.exportExcel(list, "字典类型");
//    }

    /**
     * 查询字典类型详细
     */
    @PreAuthorize("@custom.hasAnyAuthority('system:dict:query')")
    @GetMapping(value = "/{dictId}")
    public R getInfo(@PathVariable Long dictId) {
        return R.success(dictTypeService.getOne(Wrappers.<SysDictType>lambdaQuery().eq(SysDictType::getDictId,dictId)));
    }

    /**
     * 新增字典类型
     */
    @PreAuthorize("@custom.hasAnyAuthority('system:dict:add')")
//    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@Validated @RequestBody SysDictType dict) {
        if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict))) {
            return R.error("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setCreateBy(SecurityUtils.getUsername());
        return R.success(dictTypeService.insertDictType(dict) > 0);
    }

    /**
     * 修改字典类型
     */
    @PreAuthorize("@custom.hasAnyAuthority('system:dict:edit')")
//    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@Validated @RequestBody SysDictType dict) {
        if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict))) {
            return R.error("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setUpdateBy(SecurityUtils.getUsername());
        return R.success(dictTypeService.updateDictType(dict) > 0);
    }

    /**
     * 删除字典类型
     */
    @PreAuthorize("@custom.hasAnyAuthority('system:dict:remove')")
//    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictIds}")
    public R remove(@PathVariable Long[] dictIds) {
        return R.success(dictTypeService.deleteDictTypeByIds(dictIds) > 0);
    }

    /**
     * 清空缓存
     */
    @PreAuthorize("@custom.hasAnyAuthority('system:dict:remove')")
//    @Log(title = "字典类型", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clearCache")
    public R clearCache() {
        dictTypeService.clearCache();
        return R.success();
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/optionselect")
    public R optionselect() {
        List<SysDictType> dictTypes = dictTypeService.selectDictTypeAll();
        return R.success(dictTypes);
    }
}
